package wiki;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;

import com.Root;

/**
 * Servlet implementation class addissue
 */
@WebServlet("/addissue")
public class addissue extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public addissue() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		
		
		Root root = new Root();
		ResultSet rs = null;
		PreparedStatement pst=null;

		try {

			String issuetitle = "";
			String issuedetails = "";
			String bugid = "";
			String version = "";
			String moduleid = "";
			String categoryid = "";
			String subcategoryid = "";
			String module = "";
			String category = "";
			String subcategory = "";
			String action = "";
			String user="";
			
			issuetitle = request.getParameter("issuetitle");
			issuedetails = request.getParameter("issuedetails");
			bugid = request.getParameter("bugid");
			version = request.getParameter("version");
			moduleid = request.getParameter("moduleid");
			categoryid = request.getParameter("categoryid");
			subcategoryid = request.getParameter("subcategoryid");
			module = request.getParameter("module");
			category = request.getParameter("category");
			subcategory = request.getParameter("subcategory");
			user = request.getParameter("user");

			action = request.getParameter("action");

			//System.out.println("CAtegory: " + category);

			
			
			String sql, sql1 = "";
			JSONObject issueinfo = null;

			int issueid = 0;

			//System.out.println("Version: " + version);

			
			version = version.replaceAll(",", "  ,  ");

			if ((issuetitle != null) || (!issuetitle.equals(""))) {

				if (action.equalsIgnoreCase("add")) {

					root.initConnection();
					sql = "insert into issue "
							+ "(issuetitle, issuedetails, bugid, version, moduleid, categoryid, subcategoryid,module, category, subcategory,createdby, createdon )"
							+ "values (?, ?, ?,?,?, ? , ?,?, ?, ?,?, CURRENT_DATE());";

					issuedetails=issuedetails.replaceAll("\\n", "<br/>");
					
					
					pst = root.con.prepareStatement(sql);
					pst.setString(1, issuetitle);
					pst.setString(2, issuedetails);
					pst.setString(3, bugid);
					pst.setString(4, version);
					pst.setString(5, moduleid);
					pst.setString(6, categoryid);
					pst.setString(7, subcategoryid);
					pst.setString(8, module);
					pst.setString(9, category);
					pst.setString(10, subcategory);
					pst.setString(11, user);

					// System.out.println("SQL: " + pst);
					pst.executeUpdate();

					PreparedStatement pst1;
					sql1 = "select LAST_INSERT_ID() as id";
					pst1 = root.con.prepareStatement(sql1);
					// System.out.println("SQL: " + pst1);
					rs = pst1.executeQuery();
					if (rs.next()) {
						//System.out.println("Found last_insert_id !");
						issueid = rs.getInt("id");
						//System.out.println("Issueid:" + issueid);
						issueinfo = new JSONObject();
						issueinfo.put("issueid", issueid);

						out.println("{\"issueinfo\": [" + issueinfo.toString()
								+ "]}");
					} else {
						//System.out.println("Couldn't find issue named "+ issueid);
					}

				}else if ( action.equalsIgnoreCase("update")){
					
					String editissueid = request.getParameter("issueid");
					root.initConnection();
					sql = "update issue set issuetitle= ?, issuedetails= ?, bugid= ?, version= ? , moduleid= ? , categoryid= ?, subcategoryid= ?," 
					+ " module= ?, category=?, subcategory= ? where id= ?" ;


					issuedetails=issuedetails.replaceAll("\\n", "<br/>");
					
					pst = root.con.prepareStatement(sql);
					pst.setString(1, issuetitle);
					pst.setString(2, issuedetails);
					pst.setString(3, bugid);
					pst.setString(4, version);
					pst.setString(5, moduleid);
					pst.setString(6, categoryid);
					pst.setString(7, subcategoryid);
					pst.setString(8, module);
					pst.setString(9, category);
					pst.setString(10, subcategory);
					pst.setString(11, editissueid);

					// System.out.println("SQL: " + pst);
					pst.executeUpdate();
				}else{
				}

			}
		} catch (Exception e) {
			System.out.println("Exception :" + e.toString());
		}finally{
			try {
				pst.close();
				rs.close();
				root.closeConnection();
			} catch (Exception e) {
				
			}
			
		}
	}

}
